/**
 * 京东：签到、进店领豆、抽京豆
 */
auto.waitFor();
let jd = new Jd();
jd.start();

/** 京东app */
function Jd() {
    // 执行入口
    this.start = function() {
        unLock('2333');
        // 唤起京东app
        openApp();
        // 进入领京豆页面
        openGetBeanPage();
        // 抽京豆
        getBeanForLucky();
        // 进店领豆
        getBeanForShop();
        // 签到
        getBeeanForSign();
        backPage();
        // 领券
        getTicket();
        // 东东农场
        fruits();
        // 秒杀签到领豆
        getSeckillSignBean();
        home();
        log("结束");
        exit();
    }
    /** 解锁 */
    function unLock2(password) {
        const logger = new Logger('解锁');
        // 唤醒屏幕
        if (!device.isScreenOn()) {
            logger.log('唤醒设备');
            device.wakeUp();
            // 避开锁屏界面的弹窗
            backPage();
            if (text('画报')) {
                logger.log('尝试解锁')
                //下拉状态栏
                swipe(500, 30, 500, 1800, 300);
                sleep(400);
                //点击时间
                click(100, 120);
                sleep(400);
                //解锁 密码 
                if (password) {
                    password.split("").forEach(p => {
                        desc(p).findOne().click();
                    });
                }
                //等待解锁完成
                text('闹钟').waitFor();
                logger.log('解锁成功');
                //返回主页
                home();
            }
        }
    }
    /* 上滑解锁 */
    function unLock(password) {
        if (!device.isScreenOn()) {
            device.wakeUp();
            sleep(2000);
            gesture(320, [540, 1800], [540, 1000]);
            if (password) {
                password.split("").forEach(p => {
                    desc(p).findOne().click();
                    sleep(250);
                });
            }
        }
    }
    /** 退回上一页 */
    function backPage(logger) {
        back();
        if (logger) {
            logger.log("返回上一页");
        }
        sleep(2000);
    }
    /** 等待领京豆页面加载完 */
    function waitForGetBeanPage(logger) {
        text("店铺推荐").findOne();
        if (logger) {
            logger.log("页面加载完");
        }

    }
    /** 打开App */
    function openApp() {
        const appName = "京东";
        const logger = new Logger('打开' + appName);
        let targetPackageName = getPackageName(appName);
        let hasLauchApp = false;
        while (currentPackage() != targetPackageName) {
            if (!hasLauchApp) {
                home();
                sleep(1000);
                app.launchApp(appName);
                logger.log("唤起App...");
                hasLauchApp = true;
            }
            sleep(1000);
        }
        logger.log("App已启动");
        while ("com.jingdong.app.mall.MainFrameActivity" !== currentActivity()) {
            backPage();
            sleep(1000);
        }
        logger.log("完成");
    }
    /** 打开领京豆页面 */
    function openGetBeanPage() {
        const logger = new Logger('打开领京豆页面');
        sleep(3000);
        let btn = text("领京豆").findOne();
        let b = btn.bounds();
        click(b.centerX(), b.centerY());
        logger.log("点击");
        waitForGetBeanPage(logger);
        logger.log("完成");
    }
    /** 抽京豆 */
    function getBeanForLucky() {
        const logger = new Logger('抽京豆');
        waitForGetBeanPage(logger);
        let btn = text("抽京豆").findOne();
        let b = btn.bounds();
        click(b.centerX(), b.centerY());
        logger.log("点击");
        textStartsWith("今日还可以抽").findOne();
        logger.log("页面加载完成");

        let count = 0;
        while (count++ < 4) {
            if (textStartsWith("今日还可以抽 0 次哦").findOne(500)) {
                logger.log("没有抽奖机会");
                break;
            }
            logger.log("第 " + count + " 次抽奖");
            click(device.width/2, device.height/2);
            sleep(5000);
            btn = text("HfwBC8wQ1JvdRmoAAAAASUVORK5CYII=").findOne(1000);
            if (btn) {
                b = btn.bounds();
                click(b.centerX(), b.centerY());
                logger.log("领奖");
                sleep(250);
            }
        }
        backPage(logger);
        logger.log("完成");
    }
    /** 进店领豆 */
    function getBeanForShop() {
        const logger = new Logger('进店领豆');
        waitForGetBeanPage(logger);
        let btn = text("进店领豆").findOne();
        let b = btn.bounds();
        click(b.centerX(), b.centerY());
        logger.log("点击");
        text("进店有礼").findOne();
        logger.log("页面加载完成");

        let count = 0;
        while (count++ < 3) {
            btn = text("去完成").findOne(500);
            if (!btn) {
                logger.log("没有机会了");
                break;
            }
            b = btn.bounds();
            click(b.centerX(), b.centerY());
            logger.log("第 " + count + " 次进店领豆");
            sleep(1000);
            backPage(logger);
        }
        backPage(logger);
        logger.log("完成");
    }
    /** 签到领豆 */
    function getBeeanForSign() {
        const logger = new Logger('签到领豆');
        waitForGetBeanPage(logger);
        let btn = text("签到领京豆").findOne(500);
        if (btn) {
            let b = btn.bounds();
            click(b.centerX(), b.centerY());
            logger.log("点击");
            text("种豆瓜分京豆").findOne();
            logger.log("页面加载完成");
            backPage(logger);
        } else {
            logger.log("没有签到机会");
        }
        logger.log("完成");
    }
    /** 领券 */
    function getTicket() {
        const logger = new Logger('领券');
        let btn = text("领券").findOne(500);
        if (btn) {
            let b = btn.bounds();
            click(b.centerX(), b.centerY());
            logger.log("点击");
            btn = text("立即签到").findOne(4000);
            if (btn) {
                b = btn.bounds();
                click(b.centerX(), b.centerY());
                logger.log("领券");
                sleep(1000);
            } else {
                logger.log('没有找到签到按钮');
            }
            backPage(logger);
        } else {
            logger.log("没有找到入口");
        }
    }
    /** 东东农场 */
    function fruits() {
        const logger = new Logger('免费水果');
        let btn = text("免费水果").findOne(500);
        if (!btn) {
            logger.log("没有找到入口");
            return false;
        }
        let b = btn.bounds();
        click(b.centerX(), b.centerY());
        logger.log("点击");
        sleep(2000);
        btn = text('立即参与').findOnce()
        if (btn) {
            // 如果存在参与获得弹窗，则关闭
            click(960, 780);
            sleep(1000);
        }
        // 点击 签到领水
        click(130, 1640);
        sleep(1000);
        btn = text('签到领水滴').findOnce();
        if (btn) {
            btn.click();
            logger.log('签到');
            sleep(1000);
            click(980, 900);
            sleep(1000);
        } else {
            logger.log('已签到过');
            click(980, 900);
            sleep(1000);
        }
        // 浇水
        let count = 0;
        while (count++ < 10) {
            click(900, 1650);
            logger.log('第' + count + '次浇水');
            sleep(3500);
        }
        // 点击 领水滴
        click(320, 1630);
        sleep(1000);
        btn = text('去逛逛').find();
        btn.forEach((bt, index) => {
            let b = bt.bounds();
            click(b.centerX(), b.centerY());
            logger.log('逛' (index + 1) + '次');
            sleep(1000);
            back();
            sleep(1000);
        });
        btn = text('去领取').find();
        btn.forEach(bt => {
            let b = bt.bounds();
            click(b.centerX(), b.centerY());
            logger.log('去领取一次');
            sleep(1000);
        });
        btn = text('领取').find();
        btn.forEach(bt => {
            let b = bt.bounds();
            click(b.centerX(), b.centerY());
            logger.log('领取一次');
            sleep(1000);
        });
        click(1020, 800);
        sleep(1000);
        back();
        backPage(logger);
    }
    /** 秒杀签到 */
    function getSeckillSignBean() {
        const logger = new Logger('秒杀签到');
        let btn = text('京东秒杀').findOnce();
        if (!btn) {
            logger.log('没有京东秒杀入口');
            return false;
        }
        let b = btn.bounds();
        click(b.centerX(), b.centerY());
        logger.log('进入京东秒杀');
        sleep(1000);
        btn = text('品牌闪购').findOne(4000);
        if (!btn) {
            logger.log('没有品牌闪购入口');
            back();
            return false;
        }
        b = btn.bounds();
        click(b.centerX(), b.centerY());
        logger.log('进入品牌闪购');
        sleep(1000);
        btn = text('签到领京豆').findOne(4000);
        if (!btn) {
            logger.log('没有待签到按钮');
            back();
            sleep(500);
            back();
            return false;
        }
        btn.click();
        logger.log('签到领京豆');
        sleep(1000);
        back();
        sleep(500);
        back();
        sleep(500);
        back();
        sleep(500);
    }    
}

/** 日志记录器 */ 
function Logger(name) {
    this.name = name;
    this.log = function(msg) {
        console.log("【%s】: %s", this.name, msg);
    }
    this.log('开始');
}